{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-13T02:52:15.062513500Z",
     "start_time": "2024-05-13T02:52:15.053504600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.41421356 -1.41421356 -1.41421356]\n",
      " [-0.70710678 -0.70710678 -0.70710678]\n",
      " [ 0.          0.          0.        ]\n",
      " [ 0.70710678  0.70710678  0.70710678]\n",
      " [ 1.41421356  1.41421356  1.41421356]]\n",
      "-----------\n",
      "          A         B         C\n",
      "0 -1.414214 -1.414214 -1.414214\n",
      "1 -0.707107 -0.707107 -0.707107\n",
      "2  0.000000  0.000000  0.000000\n",
      "3  0.707107  0.707107  0.707107\n",
      "4  1.414214  1.414214  1.414214\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn import preprocessing\n",
    "\n",
    "# 创建一个示例DataFrame对象\n",
    "data = {'A': [1, 2, 3, 4, 5],\n",
    "        'B': [10, 20, 30, 40, 50],\n",
    "        'C': [100, 200, 300, 400, 500]}\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "df_zscore = preprocessing.scale(df)\n",
    "\n",
    "scaler = preprocessing.StandardScaler()\n",
    "\n",
    "df_trans_zscore = pd.DataFrame(scaler.fit_transform(df),columns=df.columns)\n",
    "\n",
    "print(df_zscore)\n",
    "print(\"-----------\")\n",
    "print(df_trans_zscore)\n",
    "# # 初始化StandardScaler对象\n",
    "# scaler = StandardScaler()\n",
    "# \n",
    "# # 对DataFrame对象中的数据进行Z-Score规范化\n",
    "# df_zscore = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)\n",
    "# \n",
    "# print(df_zscore)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MinMax规范化数据\n",
      "[[0.         0.         0.66666667]\n",
      " [1.         1.         1.        ]\n",
      " [0.         1.         0.        ]]\n",
      "-----------\n",
      "[[-0.70710678 -1.41421356  0.26726124]\n",
      " [ 1.41421356  0.70710678  1.06904497]\n",
      " [-0.70710678  0.70710678 -1.33630621]]\n",
      "-----------\n",
      "1.0\n",
      "[[ 0.  -0.3  0.1]\n",
      " [ 0.3  0.1  0.2]\n",
      " [ 0.   0.1 -0.1]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "import numpy as np\n",
    "# 初始化数据，每一行表示一个样本，每一列表示一个特征\n",
    "x = np.array([[ 0., -3., 1.], [ 3., 1., 2.], [ 0., 1., -1.]])\n",
    "# 使用MinMax规范话\n",
    "# 将数据进行[0,1]规范化\n",
    "min_max_scaler = preprocessing.MinMaxScaler()\n",
    "min_max_x = min_max_scaler.fit_transform(x)\n",
    "print('MinMax规范化数据')\n",
    "print(min_max_x)\n",
    "print(\"-----------\")\n",
    "scaler = preprocessing.StandardScaler()\n",
    "z_score = scaler.fit_transform(x)\n",
    "print(z_score)\n",
    "print(\"-----------\")\n",
    "# 小数定标规范化\n",
    "# 找出最大值的指数\n",
    "j = np.ceil(np.log10(np.max(abs(x))))\n",
    "print(j)\n",
    "scaler_j = x/(10**j)\n",
    "print(scaler_j)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-13T06:45:30.943193Z",
     "start_time": "2024-05-13T06:45:30.936866100Z"
    }
   },
   "id": "7330f129696001e1",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.ceil(np.log10(999)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-13T06:42:37.291188100Z",
     "start_time": "2024-05-13T06:42:37.289687100Z"
    }
   },
   "id": "69b061d34b3d4a14",
   "execution_count": 11
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
